<template>
	<view class="partyBuilding" :class="{'news_bcg':currentTab==2}">
		<view class="tabs">
			<block v-for="item in tabs" :key="item.val">
				<view class="tab_item" :class="{'current_tab':currentTab==item.val}" @tap="tabsChange(item)">
					{{item.name}}
				</view>
			</block>
		</view>
		<!-- 党建活动 -->
		<view class="activity_list" v-if="currentTab==1">
			<productList :list="list" :noMoreData="noMoreData"></productList>
		</view>

		<!-- 党史要闻 -->
		<view class="news_list" v-else-if="currentTab==2">
			<block v-for="(item,i) in list" :key="i" >
				<view class="item" @click="$to(`/pages/index/articleDetails?id=${item.id}`)">
					<image :src="item.cover" mode=""></image>
					<view class="">
						<view class="title">{{item.title}}</view>
						<view class="time">{{item.createTime}}</view>
					</view>
				</view>
			</block>
			<view style="text-align: center;" v-if="list.length==0">
				<image style="width: 200rpx;height: 200rpx;" src="../../../static/nodata.png"></image>
				<view style="color: #999;text-align: center;">暂无数据</view>
			</view>
			<view style="text-align: center;color: #999;font-size: 24rpx;" v-if="noMoreData&&list.length!=0">
				没有更多啦
			</view>
		</view>
		<!-- 党建学习 -->
		<view class="study_list" v-else>
			<view class="tabs">
				<scroll-view class="scroll-view" scroll-x="true">
					<block v-for="item in studyTabs" :key="item.id">
						<view class="tab_item" @tap="clickTabs(item)">
							<view :class="{'active_item':currentType==item.id}">{{item.categoryName}}</view>
						</view>
					</block>
				</scroll-view>
			</view>
			<view class="list">
				<block v-for="(item,i) in list" :key="i">
					<view class="item" @tap="onSkip(item)">
						<view class="num">{{i+1}}</view>
						<view class="tag">{{item.topic}}</view>
						<view class="title">{{item.paperName}}</view>
						<view class="status" v-if="item.isDone">已做</view>
					</view>
				</block>
				<view style="text-align: center;" v-if="list.length==0">
					<image style="width: 200rpx;height: 200rpx;" src="../../../static/nodata.png"></image>
					<view style="color: #999;text-align: center;">暂无数据</view>
				</view>
				<view style="text-align: center;color: #999;" v-if="noMoreData&&list.length!=0">
					没有更多啦
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import productList from "../../../components/productList/productList.vue"
	export default {
		components: {
			productList
		},
		data() {
			return {
				page: 1,
				lastPage: 1,
				noMoreData: false,
				list: [],
				tabs: [{
					name: '党建活动',
					val: 1
				}, {
					name: '党史要闻',
					val: 2
				}, {
					name: '党建学习',
					val: 3
				}],
				currentTab: 1,
				studyTabs: [],
				currentType: null,
			};
		},
		onShow() {
			this.estimate()
		},
		onReachBottom() {
			if (this.page < this.lastPage) {
				this.page += 1
				this.estimate()
			} else {
				this.noMoreData = true
			}
		},
		methods: {
			// 党史要闻
			async getPartyBuildingHistory() {
				const res = await this.$api.get('paper/new/list', {
					page: this.page,
					limit: 10,
					search: ''
				});
				// console.log(res, "党史要闻")
				if (res.code == 200) {
					const {
						list,
						totalPage
					} = res.data
					if (this.page == 1) {
						this.list = list || [];
						this.lastPage = totalPage || 1;
						// console.log(this.list, 'this.list')
					} else {
						this.list.push(...list);
					}
					this.page != 1 && list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}
			},
			// 党建活动
			async getPartyBuildingActivities() {
				const res = await this.$api.get('competition/week/list', {
					activity: 1, //活动 0=赛事, 1=党建，2=团建，3=研学产品，4=冬夏令营，5=红领巾博物馆 6=托管服务
					page: this.page,
					limit: 10,
					type: 0, //0=本周 1=往期
				});
				// console.log(res, "活动")
				if (res.code == 200) {
					const {
						list,
						totalPage
					} = res.data
					if (this.page == 1) {
						this.list = list || [];
						this.lastPage = totalPage || 1;
						// console.log(this.list, 'this.list')
					} else {
						this.list.push(...list);
					}
					this.page != 1 && list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}
			},

			onSkip(item) {
				if (item.isDone) {
					this.$to(`/pages/index/evaluationResult?id=${item.id}&pagetype=党建学习&joinPaperId=${item.joinPaperId}`)
				} else {
					this.$to(`/pages/index/test/test?id=${item.id}&pagetype=党建学习`)
				}
			},
			// 党建分类
			async getPartyBuildingClassify() {
				const res = await this.$api.get('paper/paper/category', {});
				if (res.code == 200) {
					// console.log(res, "%%%%%")
					this.studyTabs = res.data
					this.currentType = this.studyTabs[0].id
					// console.log(this.currentType)
				}
			},
			// 党建学习列表
			async getBuildingTest() {
				const res = await this.$api.get('paper/study/list', {
					categoryId: this.currentType,
					page: this.page,
					limit: 10,
					type: 0, //0-党建学习 1-心理测评
				});
				// console.log(res, "11111--")
				if (res.code == 200) {
					const {
						list,
						totalPage
					} = res.data
					if (this.page == 1) {
						this.list = list || [];
						this.lastPage = totalPage || 1;
						// console.log(this.list, 'this.list')
					} else {
						this.list.push(...list);
					}
					this.page != 1 && list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}

			},
			async estimate() {
				if (this.currentTab == 1) {
					this.getPartyBuildingActivities()
				}
				if (this.currentTab == 2) {
					this.getPartyBuildingHistory()
				}
				if (this.currentTab == 3) {
					await this.getPartyBuildingClassify()
					this.getBuildingTest()
				}
			},

			tabsChange(item) {
				this.currentTab = item.val
				this.page = 1
				this.list = []
				this.noMoreData = false
				this.estimate()
			},
			clickTabs(item) {
				this.currentType = item.id
			},


		}
	}
</script>

<style lang="scss">
	page {
		height: 100vh;
		background-color: #f7f7f7;

	}

	.activity_list {
		width: 100%;
		padding: 0 26rpx 30rpx;
		box-sizing: border-box;
		position: absolute;
		left: 0;
		right: 0;
		top: 120rpx;
		z-index: 3;
	}

	.partyBuilding {

		.tabs {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			padding: 26rpx;
			box-sizing: border-box;
			position: fixed;
			left: 0;
			// #ifdef H5 
			top: 80rpx;
			// #endif
			right: 0;
			z-index: 9;

			.tab_item {
				font-size: 26rpx;
			}

			.current_tab {
				font-size: 30rpx;
				font-weight: bold;
				position: relative;
			}

			.current_tab::before {
				height: 8rpx;
				width: 100%;
				background-image: linear-gradient(to right, #3DD8FF, #589AFF);
				position: absolute;
				display: block;
				content: '';
				bottom: 0;
				left: 0;
				// z-index: -1;
			}
		}

		.news_list {
			padding: 90rpx 26rpx 30rpx;
			box-sizing: border-box;

			.item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				background-color: #fff;
				padding: 48rpx 0 24rpx;
				border-bottom: 1rpx solid #F5F5F5;

				image {
					width: 200rpx;
					height: 146rpx;
					border-radius: 6rpx;
					margin-right: 18rpx;
				}

				>view {
					flex: 1;
					height: 146rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.title {
						font-size: 26rpx;
						font-weight: bold;
						line-height: 40rpx;
					}

					.time {
						font-size: 24rpx;
						color: #B3B3B3;
					}
				}
			}
		}

		.study_list {
			.tabs {
				padding: 0 26rpx;
				box-sizing: border-box;
				height: 74rpx;
				overflow: hidden;
				display: flex;
				align-items: center;
				position: fixed;
				left: 0;
				right: 0;

				top: 92rpx;
				// #ifdef H5
				top: 160rpx;
				// #endif
				z-index: 6;

				.scroll-view {
					white-space: nowrap;
					width: 100%;

					.tab_item {
						padding: 30rpx 0;
						display: inline-block;

						>view {
							height: 46rpx;
							line-height: 40rpx;
							border-radius: 23rpx;
							padding: 0 24rpx;
							background-color: #F0F0F0;
							font-size: 24rpx;
							color: #999999;
						}

						.active_item {
							background-color: #E8F1FF;
							color: #2E80FE;
							font-weight: bold;
						}
					}

					.tab_item:not(:last-child) {
						margin-right: 38rpx;
					}
				}
			}

			.list {
				padding: 200rpx 26rpx 30rpx;
				box-sizing: border-box;

				.item {
					width: 100%;
					height: 106rpx;
					background-color: #fff;
					border-radius: 14rpx;
					padding: 0 30rpx;
					box-sizing: border-box;
					display: flex;
					align-items: center;
					position: relative;
					margin-bottom: 20rpx;

					.num {
						font-size: 32rpx;
						font-weight: bold;
						color: #1A1A1A;
					}

					.tag {
						width: 80rpx;
						height: 36rpx;
						font-size: 22rpx;
						text-align: center;
						line-height: 36rpx;
						border-radius: 6rpx;
						background-color: #FDF1E1;
						color: #EA9017;
						margin: 0 26rpx 0 22rpx;
					}

					.tag2 {
						background-color: #DDFFF7;
						color: #1CC19C;
					}

					.title {
						font-size: 30rpx;
						font-weight: bold;
						color: #1A1A1A;
					}

					.status {
						width: 94rpx;
						height: 46rpx;
						border-radius: 0 14rpx 0 14rpx;
						text-align: center;
						line-height: 46rpx;
						font-size: 24rpx;
						color: #EF791F;
						position: absolute;
						top: 0;
						right: 0;
						background-color: #FFE5D1;
					}
				}
			}
		}
	}

	.news_bcg {
		min-height: 100%;
		background-color: #fff;
	}
</style>